<template>
  <div>
    <div>【大区数据信息】</div>
    <div ref="horizontalBarChart" class="w-full h-full"></div>
  </div>
</template>

<script setup>
import { ref, onMounted, watch } from 'vue'
import * as echarts from 'echarts'
const props = defineProps({
  data: {
    type: Object,
    required: true,
  },
})
const horizontalBarChart = ref(null)
let mcharts = null
onMounted(() => {
  mcharts = echarts.init(horizontalBarChart.value)
  renderChart()
})

const renderChart = () => {
  const options = {
    xAxis: {
      show: false,
      type: 'value',
      max: (value) => {
        return parseInt(value.max * 1.2)
      },
    },
    yAxis: {
      type: 'category',
      data: props.data.regions.map((item) => item.name),
      inver: true,
      axisTick: {
        show: false,
      },
      axisLine: {
        show: false,
      },
      axisLabel: {
        color: '#9eb1c8',
      },
    },
    grid: {
      top: 0,
      bottom: 0,
      left: 0,
      right: 0,
      containLabel: true,
    },
    series: [
      {
        type: 'bar',
        data: props.data.regions.map((item) => ({
          name: item.name,
          value: item.value,
        })),
        showbackground: true,
        backgroundStyle: {
          color: 'rgba(180, 180, 180, 0.2)',
        },
        itemStyle: {
          borderRadius: 5,
          color: '#5d98ce',
          shadowColor: 'rgba(0, 0, 0, 0.3)',
          shadowBlur: 5,
        },
        barWidth: 12,
        label: {
          show: true,
          position: 'right',
          textStyle: {
            color: '#fff',
          },
        },
      },
    ],
  }
  mcharts.setOption(options).resize()
}

watch(
  () => props.data,
  () => {
    renderChart()
  },
)
</script>

<style lang="scss" scoped></style>
